package com.wxl.xueyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.wxl.xueyuan.MyApp;
import com.wxl.xueyuan.R;
import com.wxl.xueyuan.api.Api;
import com.wxl.xueyuan.api.ApiConfig;
import com.wxl.xueyuan.api.FileUtils;
import com.wxl.xueyuan.api.MCallback;
import com.wxl.xueyuan.util.ActivityCollectorUtil;
import com.wxl.xueyuan.util.StringUtils;

import java.util.HashMap;

public class SettingActivity extends BaseActivity {

    private EditText etint;
    private EditText etname;
    private Button conf;
    private Button jnews;
    private Button jtz;
    private Button rnews;
    private Button rtz;
    private Button jwcnews;
    private Button jwctz;
    private ImageButton back;

    @Override
    protected int initLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        etint=findViewById(R.id.et_int);
        etname=findViewById(R.id.et_name);
        conf=findViewById(R.id.btn_conf);
        jnews=findViewById(R.id.btn_jnews);
        jtz=findViewById(R.id.btn_jtz);
        rnews=findViewById(R.id.btn_rnews);
        rtz=findViewById(R.id.btn_rtz);
        jwcnews=findViewById(R.id.btn_jwcnews);
        jwctz=findViewById(R.id.btn_jwctz);
        back=findViewById(R.id.back);
        ActivityCollectorUtil.addActivity(SettingActivity.this);
    }
    protected void setBt(Button b,int s){
        if(s==1){
            b.setBackgroundColor(0xc75fe768);
            b.setText("开启");
        }
        else{
            b.setBackgroundColor(0xafee5d59);
            b.setText("关闭");
        }
    }
    @Override
    protected void initData() {
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        etint.setText(bundle.getString("etint"));
        etname.setText(bundle.getString("etname"));
        setBt(jnews, (Integer) MyApp.getmData("cg0"));
        setBt(jtz, (Integer) MyApp.getmData("cg1"));
        setBt(rnews, (Integer) MyApp.getmData("cg2"));
        setBt(rtz, (Integer) MyApp.getmData("cg3"));
        setBt(jwcnews, (Integer) MyApp.getmData("cg4"));
        setBt(jwctz, (Integer) MyApp.getmData("cg5"));
        jnews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg0") == 1){
                    MyApp.setmData("cg0", 0);
                    setBt(jnews,0);
                }
                else{
                    MyApp.setmData("cg0", 1);
                    setBt(jnews,1);
                }
            }
        });
        jtz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg1") == 1){
                    MyApp.setmData("cg1", 0);
                    setBt(jtz,0);
                }
                else{
                    MyApp.setmData("cg1", 1);
                    setBt(jtz,1);
                }
            }
        });
        rnews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg2") == 1){
                    MyApp.setmData("cg2", 0);
                    setBt(rnews,0);
                }
                else{
                    MyApp.setmData("cg2", 1);
                    setBt(rnews,1);
                }
            }
        });
        rtz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg3") == 1){
                    MyApp.setmData("cg3", 0);
                    setBt(rtz,0);
                }
                else{
                    MyApp.setmData("cg3", 1);
                    setBt(rtz,1);
                }
            }
        });
        jwcnews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg4") == 1){
                    MyApp.setmData("cg4", 0);
                    setBt(jwcnews,0);
                }
                else{
                    MyApp.setmData("cg4", 1);
                    setBt(jwcnews,1);
                }
            }
        });
        jwctz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((Integer) MyApp.getmData("cg5") == 1){
                    MyApp.setmData("cg5", 0);
                    setBt(jwctz,0);
                }
                else{
                    MyApp.setmData("cg5", 1);
                    setBt(jwctz,1);
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.super.onBackPressed();
            }
        });
        conf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=etname.getText().toString();
                String intr=etint.getText().toString();
                HashMap n = new HashMap();
                HashMap i = new HashMap();
                n.put("name", name);
                n.put("ID", MyApp.getmData("ID"));
                i.put("intro", intr);
                i.put("ID", MyApp.getmData("ID"));
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Api.config("/user/setname", n).getRequest(SettingActivity.this, new MCallback() {
                            @Override
                            public void onSuccess(String res) {
                                System.out.println("修改昵称成功");
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
                        Api.config("/user/setintro", i).getRequest(SettingActivity.this, new MCallback() {
                            @Override
                            public void onSuccess(String res) {
                                System.out.println("修改个人介绍成功");
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
                        navigateTo(HomeActivity.class);
                    }
                }).start();

            }
        });
    }
}